/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.controllers;

import com.mycompany.psc.ejb.ReportableItemsManager;
import com.mycompany.psc.entities.Aluminium;
import com.mycompany.psc.entities.AluminiumOffers;
import com.mycompany.psc.entities.Users;
import com.mycompany.psc.producers.LoggedIn;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author ahmed
 */
@Named
@SessionScoped
public class AluminuimController implements Serializable {

    @EJB
    ReportableItemsManager rm;
    @Inject
    ProjectsBean p;
    private String title;
    private String code;
    @Inject
    @LoggedIn
    Users current;
    private Aluminium selectedResource;
    private AluminiumOffers newItem = new AluminiumOffers();
    private AluminiumOffers editableItem;

    public AluminiumOffers getNewItem() {
        return newItem;
    }

    public void setNewItem(AluminiumOffers newItem) {
        this.newItem = newItem;
    }
    private float rate;
    private int amount;
    private double aluRate;
    private String dimension;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
public AluminiumOffers getEditableItem() {
        return editableItem;
    }

    public void setEditableItem(AluminiumOffers editableItem) {
        this.editableItem = editableItem;
    }
    public Aluminium getSelectedResource() {
        return selectedResource;
    }

    public void setSelectedResource(Aluminium selectedResource) {
        this.selectedResource = selectedResource;
    }

    public float getRate() {
        return rate;
    }

    public void setRate(float rate) {
        this.rate = rate;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public double getAluRate() {
        return aluRate;
    }

    public void setAluRate(double aluRate) {
        this.aluRate = aluRate;
    }

    public String getDimension() {
        return dimension;
    }

    public void setDimension(String dimension) {
        this.dimension = dimension;
    }

    public void save() {

        rm.saveAluminium(p.getSelectedReportProject(), current, title, dimension, amount, rate, aluRate, code);
    }

    public void saveData() {
        newItem.setParent(getSelectedResource());
        getSelectedResource().getAluminiumOffersList().add(newItem);

        rm.update(getSelectedResource());
    }

    public void updateItem() {
        rm.update(selectedResource);
    }

    public void updateChildItem() {
        rm.update(editableItem);
    }

    public List<Aluminium> getAluminiumResources() {
        return rm.getAluminiumResources(p.getSelectedReportProject());
    }

    public List<AluminiumOffers> getChildResources() {
        List<Aluminium> list = rm.getAluminiumResources(p.getSelectedReportProject());
        List<AluminiumOffers> out = new ArrayList<>();
        for (Aluminium ti : list) {
            out.addAll(ti.getAluminiumOffersList());
        }
        return out;
    }
}
